package com.myapplication.zhou.recycleviewscroll;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {
    RecyclerView recyclerView;
    private LinearLayoutManager linearLayoutManager;
    private com.myapplication.zhou.recycleviewscroll.recycleViewAdapter recycleViewAdapter;
    private List<Object> list = new ArrayList<>();
    private boolean states = false;
    private int lastVisibleItemPosition;
    private footer footer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        linearLayoutManager = new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(linearLayoutManager);
        recycleViewAdapter = new recycleViewAdapter(this, list);
        recyclerView.addOnScrollListener(onScrollListener);
        recyclerView.setAdapter(recycleViewAdapter);
        List<Object> list1 = new ArrayList<>();
        for (int i = 0; i <= 20; i++) {
            model m = new model();
            m.setText("ksksk"+i);
            list1.add(m);
        }
        list.addAll(list1);
        recycleViewAdapter.notifyDataSetChanged();
    }

    RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            recycleViewOnScroll(newState);
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            lastVisibleItemPosition = linearLayoutManager.findLastCompletelyVisibleItemPosition();
        }
    };

    private void recycleViewOnScroll(int newState) {
        if (list.size() ==0) {

        } else if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
            if(!states) {
                states = true;
                onloadmore();

            }
        }
    }

    private void onloadmore() {
        footer = new footer();
        footer.setFooter("jsjsjjsjs");
        list.add(footer);
        recycleViewAdapter.notifyDataSetChanged();
        MyHandler m = new MyHandler();
        Message messge = new Message();
        messge.what =1;


    }

    class MyHandler extends Handler{
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            this.sendEmptyMessageDelayed(0,3000);
            onPostLoad();
        }
    }
    private void onPostLoad() {
        List<Object> list1 = new ArrayList<>();
        for (int i = 0; i <= 20; i++) {
            model m = new model();
            m.setText("ksksk");
            list1.add(m);
        }
        list.addAll(list1);
        list.remove(footer);
        recycleViewAdapter.notifyDataSetChanged();
        states = false;
    }

    private void delay(int ms) {
        try {
            Thread.currentThread();
            Thread.sleep(ms);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

